home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / util / max / impflybsp / impflybsp.cpp next >
Encoding:
C/C++ Source or Header  |  2000-02-15  |  6.9 KB  |  320 lines

  1. #include "Max.h"
  2. #include "Stdmat.h"
  3.  
  4. #include "../../../lib/Fly3D.h"
  5.  
  6. #include "resource.h"
  7.  
  8. #define IMPFLYBSP_CLASS_ID    Class_ID(0x3a9289d2, 0x82f062be)
  9.  
  10. ClassDesc* GetimpflybspDesc();
  11.  
  12. int controlsInit = FALSE;
  13. HINSTANCE hInstance;
  14.  
  15. BOOL WINAPI DllMain(HINSTANCE hinstDLL,ULONG fdwReason,LPVOID lpvReserved)
  16. {
  17.     hInstance = hinstDLL;                // Hang on to this DLL's instance handle.
  18.  
  19.     if (!controlsInit) {
  20.         controlsInit = TRUE;
  21.         InitCustomControls(hInstance);    // Initialize MAX's custom controls
  22.         InitCommonControls();            // Initialize Win95 controls
  23.     }
  24.             
  25.     return (TRUE);
  26. }
  27.  
  28. TCHAR *GetString(int id)
  29. {
  30.     static TCHAR buf[256];
  31.  
  32.     if (hInstance)
  33.         return LoadString(hInstance, id, buf, sizeof(buf)) ? buf : NULL;
  34.     return NULL;
  35. }
  36.  
  37. __declspec( dllexport ) const TCHAR* LibDescription()
  38. {
  39.     return GetString(IDS_LIBDESCRIPTION);
  40. }
  41.  
  42. __declspec( dllexport ) int LibNumberClasses()
  43. {
  44.     return 1;
  45. }
  46.  
  47. __declspec( dllexport ) ClassDesc* LibClassDesc(int i)
  48. {
  49.     switch(i) {
  50.         case 0: return GetimpflybspDesc();
  51.         default: return 0;
  52.     }
  53. }
  54.  
  55. __declspec( dllexport ) ULONG LibVersion()
  56. {
  57.     return VERSION_3DSMAX;
  58. }
  59.  
  60. class impflybsp : public SceneImport, public flyEngine {
  61.     public:
  62.  
  63.     int *vertflag;
  64.  
  65.     //Constructor/Destructor
  66.     impflybsp();
  67.     ~impflybsp();
  68.  
  69.     int                ExtCount();                    // Number of extensions supported
  70.     const TCHAR *    Ext(int n);                    // Extension #n (i.e. "3DS")
  71.     const TCHAR *    LongDesc();                    // Long ASCII description (i.e. "Autodesk 3D Studio File")
  72.     const TCHAR *    ShortDesc();                // Short ASCII description (i.e. "3D Studio")
  73.     const TCHAR *    AuthorName();                // ASCII Author name
  74.     const TCHAR *    CopyrightMessage();            // ASCII Copyright message
  75.     const TCHAR *    OtherMessage1();            // Other message #1
  76.     const TCHAR *    OtherMessage2();            // Other message #2
  77.     unsigned int    Version();                    // Version number * 100 (i.e. v3.01 = 301)
  78.     void            ShowAbout(HWND hWnd);        // Show DLL's "About..." box
  79.     int                DoImport(const TCHAR *name,ImpInterface *i,Interface *gi, BOOL suppressPrompts=FALSE);    // Import file
  80.     void            build_mesh(TriObject *obj,int flag);
  81.     void            select_bsp_faces(bsp_node *n);
  82. };
  83.  
  84. class impflybspClassDesc:public ClassDesc {
  85.     public:
  86.     int             IsPublic() {return 1;}
  87.     void *            Create(BOOL loading = FALSE) {return new impflybsp();}
  88.     const TCHAR *    ClassName() {return GetString(IDS_CLASS_NAME);}
  89.     SClass_ID        SuperClassID() {return SCENE_IMPORT_CLASS_ID;}
  90.     Class_ID        ClassID() {return IMPFLYBSP_CLASS_ID;}
  91.     const TCHAR*     Category() {return GetString(IDS_CATEGORY);}
  92.     void            ResetClassParams (BOOL fileReset);
  93. };
  94.  
  95. static impflybspClassDesc impflybspDesc;
  96. ClassDesc* GetimpflybspDesc() {return &impflybspDesc;}
  97.  
  98. void impflybspClassDesc::ResetClassParams (BOOL fileReset) 
  99. {
  100. }
  101.  
  102. impflybsp::impflybsp()
  103. {
  104.  
  105. }
  106.  
  107. impflybsp::~impflybsp() 
  108. {
  109.  
  110. }
  111.  
  112. int impflybsp::ExtCount()
  113. {
  114.     return 1;
  115. }
  116.  
  117. const TCHAR *impflybsp::Ext(int n)
  118. {
  119.     return _T("BSP");
  120. }
  121.  
  122. const TCHAR *impflybsp::LongDesc()
  123. {
  124.     return _T("Fly3D BSP File");
  125. }
  126.     
  127. const TCHAR *impflybsp::ShortDesc() 
  128. {
  129.     return _T("Fly3D BSP");
  130. }
  131.  
  132. const TCHAR *impflybsp::AuthorName()
  133. {
  134.     return _T("Paralelo Computaτπo Ltda.");
  135. }
  136.  
  137. const TCHAR *impflybsp::CopyrightMessage() 
  138. {
  139.     return _T("");
  140. }
  141.  
  142. const TCHAR *impflybsp::OtherMessage1() 
  143. {
  144.     return _T("");
  145. }
  146.  
  147. const TCHAR *impflybsp::OtherMessage2() 
  148. {
  149.     return _T("");
  150. }
  151.  
  152. unsigned int impflybsp::Version()
  153. {
  154.     return 100;
  155. }
  156.  
  157. void impflybsp::ShowAbout(HWND hWnd)
  158. {
  159. }
  160.  
  161. void impflybsp::select_bsp_faces(bsp_node *n)
  162. {
  163.     if(n->child[0])
  164.         select_bsp_faces(n->child[0]);
  165.  
  166.     if(n->child[1])
  167.         select_bsp_faces(n->child[1]);
  168.  
  169.     if(n->elem)
  170.     {
  171.         static_mesh *o=(static_mesh *)n->elem;
  172.  
  173.         for(int i=0;i<o->objmesh->nf;i++)
  174.         {
  175.             o->objmesh->faces[i]->indx=1;
  176.             vertflag[o->objmesh->faces[i]->vert[0]-vert]=1;
  177.             vertflag[o->objmesh->faces[i]->vert[1]-vert]=1;
  178.             vertflag[o->objmesh->faces[i]->vert[2]-vert]=1;
  179.         }
  180.     }
  181. }
  182.  
  183.  
  184. void impflybsp::build_mesh(TriObject *obj, int flag)
  185. {
  186.     int i, i0, j, nf, nv;
  187.  
  188.     for(i=0; i<nfaces;i++ )
  189.         faces[i].indx=0;
  190.  
  191.     for(i=0; i<nvert;i++ )
  192.         vertflag[i]=0;
  193.  
  194.     select_bsp_faces(bsp);
  195.  
  196.     nf=0;
  197.     for(i=0; i<nfaces;i++ )
  198.         if(faces[i].indx==flag) nf++;
  199.  
  200.     nv=0;
  201.     for(i=0; i<nvert;i++ )
  202.         if(vertflag[i]==flag) nv++;
  203.  
  204.     obj->mesh.setNumFaces(nf);
  205.     obj->mesh.setNumVerts(nv);
  206.     obj->mesh.setNumTVFaces(nf);
  207.     obj->mesh.setNumTVerts(nf*3);
  208.  
  209.     i0=0;
  210.     for( i=0;i<nvert;i++ )
  211.     if(vertflag[i]==flag)
  212.     {
  213.         obj->mesh.setVert(i0,vert[i].x,vert[i].y,vert[i].z);
  214.         vertflag[i]=i0;
  215.         i0++;
  216.     }
  217.  
  218.     i0=0;
  219.     for( i=0;i<nfaces;i++ )
  220.     if(faces[i].indx==flag)
  221.     {
  222.     for( j=0;j<3;j++ )
  223.         {
  224.         obj->mesh.tVerts[i0*3+j].x=faces[i].uv[j][0];
  225.         obj->mesh.tVerts[i0*3+j].y=-faces[i].uv[j][1];
  226.         obj->mesh.tVerts[i0*3+j].z=0;
  227.         obj->mesh.faces[i0].v[j]=vertflag[faces[i].vert[j]-vert];
  228.         obj->mesh.tvFace[i0].t[j]=i0*3+j;
  229.         }
  230.     obj->mesh.faces[i0].flags=EDGE_ALL;
  231.     obj->mesh.faces[i0].smGroup=0;
  232.     obj->mesh.faces[i0].setMatID(faces[i].texpic+1);
  233.     i0++;
  234.     }
  235. }
  236.  
  237. int impflybsp::DoImport(const TCHAR *filename,ImpInterface *i,
  238.                         Interface *gi, BOOL suppressPrompts)
  239. {
  240.     char file[256],str[256],flypathname[256];
  241.     strcpy(flypathname,filename);
  242.     *(strrchr(flypathname,'\\')+1)=0;
  243.     strcpy(file,strrchr(filename,'\\')+1);
  244.     *strchr(file,'.')=0;
  245.  
  246.     strcpy(str,flypathname);
  247.     strcat(str,file);
  248.     strcat(str,".bsp");
  249.  
  250.     load_bsp(str);
  251.  
  252.     strcpy(str,flypathname);
  253.     strcat(str,file);
  254.     strcat(str,".tex");
  255.  
  256.     load_pictures(str);
  257.  
  258.     vertflag=new int[nvert];
  259.  
  260.     ImpNode *node=i->CreateNode();
  261.     node->SetName(_T("flybsp"));
  262.  
  263.     TriObject *obj=(TriObject *)i->Create(GEOMOBJECT_CLASS_ID,Class_ID(TRIOBJ_CLASS_ID,0));
  264.     node->Reference(obj);
  265.  
  266.     build_mesh(obj,1);
  267.  
  268.     MultiMtl *mm=(MultiMtl *)i->Create(MATERIAL_CLASS_ID,Class_ID(MULTI_CLASS_ID,0));
  269.     mm->SetName(TSTR(_T("flybsp1")));
  270.     mm->SetNumSubMtls(npiclib+1);
  271.     int a,b=1;
  272.     for( a=1;a<=npiclib;a++ )
  273.     {
  274.         StdMat *mat=(StdMat *)i->Create(MATERIAL_CLASS_ID,Class_ID(DMTL_CLASS_ID, 0));
  275.         mat->SetAmbient(Color(0.2f,0.2f,0.2f),0);
  276.         mat->SetDiffuse(Color(1.0f,1.0f,1.0f),0);
  277.         mat->SetSpecular(Color(1.0f,1.0f,1.0f),0);
  278.         mat->SetShininess(0,0);
  279.         mat->SetShinStr(0,0);
  280.         mat->SetTwoSided(FALSE);
  281.         mat->SetTexmapAmt(ID_DI,1,0);
  282.         mat->EnableMap(ID_DI,TRUE);
  283.         sprintf(str,"flybspmat%02i",b++);
  284.         mat->SetName(TSTR(_T(str)));
  285.         if (piclib[a-1] && piclib[a-1]->name[0])
  286.             {
  287.             BitmapTex *tex1=(BitmapTex *)i->Create(TEXMAP_CLASS_ID,Class_ID(BMTEX_CLASS_ID,0));
  288.             tex1->SetMapName(piclib[a-1]->name);
  289.             mat->SetSubTexmap(ID_DI, tex1);
  290.             }
  291.         mm->SetSubMtl(a,mat);
  292.     }
  293.     node->GetINode()->SetMtl(mm);
  294.     i->AddNodeToScene(node);
  295.  
  296.     for( a=0;a<nfaces;a++ )
  297.         if (faces[a].indx==0)
  298.             break;
  299.     if (a<nfaces)
  300.         {
  301.         node=i->CreateNode();
  302.         node->SetName(_T("flybsp2"));
  303.         
  304.         obj=(TriObject *)i->Create(GEOMOBJECT_CLASS_ID,Class_ID(TRIOBJ_CLASS_ID,0));
  305.         node->Reference(obj);
  306.  
  307.         build_mesh(obj,0);
  308.         
  309.         node->GetINode()->SetMtl(mm);
  310.         i->AddNodeToScene(node);
  311.         }
  312.  
  313.     delete vertflag;
  314.  
  315.     i->RedrawViews();
  316.  
  317.     return TRUE;
  318. }
  319.     
  320.